<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

class AgeController extends Controller
{

    public function __construct()
    {
        //$this->middleware('checkAge');
        $this->middleware(function ($request, $next) {
            if($request->age>=100){
                return redirect('/')->with('err', 'age大于100');
                exit;
            }
            return $next($request);
        });
    }

    public function __invoke(Request $request){
        $messages = [
            'required' => ':attribute值不能为空！',
        ];
        $this->validate($request, [
        //$validator = Validator::make($request->all(),[
            'name' => 'required',
            'age' => 'required',
        ],$messages);
        /*if ($validator->fails()) {
            return redirect('admin')
                ->withErrors($validator)
                ->withInput();
        }*/
        if(Input::ajax()){
            //session()->pull('key', 'default');检索删除
            //$request->session()->forget('key');从 Session 内删除一条数据
            //$request->session()->flush();删除 Session 内所有数据
            if($request->session()->has('key')){
                $ses=true;
            }else{
                $ses=false;
            }
            $session = $request->session()->all();
            $input = Input::all();
            $uri = $request->fullUrl();
            return response()->json([
                    'input' => $input,
                    'uri' => $uri,
                    //'session' => $session,
                    'ses'=>$ses
                ]
            );
        }
    }
}
